//
//  ViewController.m
//  TestView
//
//  Created by wsj2015 on 16/8/30.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "BTMarketVC.h"
#import "TagGridView.h"
#import "HMSegmentedControl.h"
#import "YFLinkageScrollView.h"
#import "BTVCTableView.h"
#import "BTCourseFlagModel.h"
#import "CWQ_historySearchVC.h"
#import "BTChooseCourseVC.h"
@interface BTMarketVC ()<YFLinkageScrollViewDelegate>

@property (strong, nonatomic) YFLinkageScrollView *yfScrollView;

@property (nonatomic, strong) NSMutableArray *tagArr;
@property (nonatomic, strong) NSMutableArray *viewCtrls;

@property (nonatomic,  assign) NSInteger currentIndex;

@end

@implementation BTMarketVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

 
    self.title = @"市场";
    [self requestCourseFlags];
      UIBarButtonItem *right1 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"筛选_icon.png"] style:0 target:self action:@selector(repick)];
    UIBarButtonItem *right2 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索_icon.png"] style:0 target:self action:@selector(toSearch)];
    
    self.navigationItem.rightBarButtonItems = @[right2,right1];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.currentIndex = 0;
    
    self.viewCtrls = [NSMutableArray array];
    
}
-(void)toSearch{
    
    CWQ_historySearchVC *vc =[CWQ_historySearchVC new];
    vc.searchType = CWQ_historySearchVCType_Course_Market;
    //wsjTest
    vc.flagId = 2;
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)repick{
    weakSelf_define;
    [BTChooseCourseVC showWithVC:self.navigationController WithDoneBlock:^(BTCourseQueryTypeModel *typeModel) {
        if (weakSelf.viewCtrls.count<1) {
            return;
        }
        BTVCTableView *vc = self.viewCtrls[weakSelf.currentIndex];
        vc.courseCondtionModel = typeModel;
        [vc  requestMarketCourseList:YES];
    }];
    
}
- (void)getData_configUI{
    
  
    
weakSelf_define;
    
    
    [self.tagArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BTVCTableView *tb = [[BTVCTableView alloc] init];
        BTCourseFlagModel *model = weakSelf.dataArray[idx];
        tb.flagId = [model.id integerValue];
        [weakSelf.viewCtrls addObject:tb];
        if (idx==0) {
            [tb requestData];
        }
        
    }];
    
    
    self.yfScrollView = [[YFLinkageScrollView alloc] init];
    self.yfScrollView.frame  =CGRectMake(0, 0, self.view.bounds.size.width ,kScreenHeight);
    [self.view addSubview:self.yfScrollView];
    
    
    
    UIView *sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 4)];
    sliderView.layer.cornerRadius = .5;
    sliderView.backgroundColor = kMainColor;
    
    [self.yfScrollView configWithScrolltagArray:self.tagArr tagScrollEdgeInset:UIEdgeInsetsMake(0, 0, 0, 33) tagScale:1.1 configTagItemBlock:nil  visibleCount:4 sliderType:YFSliderTypeBottomAlone customSlider:sliderView contentScrollItem:self.viewCtrls];
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(self.view.bounds.size.width -34, 8,34 , 33);
    [btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"indecator_up"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(5,3, 3, 6);
//    btn.transform = CGAffineTransformRotate(btn.transform, M_PI);
    [self.view addSubview:btn];
    
    self.yfScrollView.rotateVisibleCount = 5;
    self.yfScrollView.tagScroll.backgroundColor = [UIColor whiteColor];
    //    self.yfScrollView.isMoveToVisible = YES;
    self.yfScrollView.sliderWidthScale = .3;
    self.yfScrollView.delegate = self;
    
    self.yfScrollView.tagScroll.tagColorNor = [UIColor blackColor];
    self.yfScrollView.tagScroll.tagColorSelect = kMainColor;
    self.yfScrollView.sliderColor = kBorderColor;
    
    BTVCTableView *vc = self.viewCtrls.firstObject;
    [vc viewWillAppear:YES];
}

- (NSMutableArray *)tagArr{
    if (!_tagArr) {
        _tagArr = [NSMutableArray array];
    }
    return _tagArr;
}

- (void)requestCourseFlags{
      weakSelf_define;
    
    [weakSelf wsj_showLodingGif];
    [[BTNetworkManager sharedMg] POST:kMarketFlagsOrMarketCourseList parameters:nil success:^(NSDictionary *responseObject) {
         [weakSelf wsj_hideLodingGif];

            if (responseObject_success) {
                //总页数
    
            [weakSelf.dataArray addObjectsFromArray:[BTCourseFlagModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"flagList"]]];
           
                
            //wsjtest
//                BTCourseFlagModel *model = weakSelf.dataArray.firstObject;
//                [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
//                 [weakSelf.dataArray addObject:model];
                
            [weakSelf.dataArray enumerateObjectsUsingBlock:^(BTCourseFlagModel * _Nonnull  obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [weakSelf.tagArr addObject:obj.flagName];
               
            }];
            [weakSelf getData_configUI];
            }else{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
       
    } failure:^(NSString *err) {
         [weakSelf wsj_hideLodingGif];
     
    }];
    

    
}


- (void)yfScrollViewChangeCurrentIndex:(NSInteger)currentIndex item:(id)item{
   self.currentIndex = currentIndex;
    
    BTVCTableView *vc = self.viewCtrls[currentIndex];
    [vc  requestData];
    
}


- (IBAction)show:(id)sender {
    
//    static int i = 0;
//    i++;
//      [self.yfScrollView setCurrentIndex:i animated:NO TagAnimated:NO];
    TagGridView *tg = [TagGridView gridViewWithDataArray:self.tagArr];
    tg.selectIndex = self.currentIndex;
    weakSelf_define;
    tg.callBack = ^(NSInteger index){
     
        [weakSelf.yfScrollView setCurrentIndex:index animated:NO TagAnimated:NO];
        
    
    };
    
    [tg showInView:self.view];
}



@end
